package zhihuilan.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import zhihuilan.dto.MyCircleDto;
import zhihuilan.dto.MySearchDto;
import zhihuilan.entity.MyCircle;
import zhihuilan.entity.MyConfig;
import zhihuilan.entity.MySearch;
import zhihuilan.entity.MyStaffShopGoods;
import zhihuilan.params.query.MyCircleQueryParam;

import java.util.List;


@Mapper
public interface MyCircleMapper extends BaseMapper<MyCircle> {
    @Select({" <script> select a.*,a.id pid,a.image img,min(${orderStr})  min_priority,a.title name,b.logo as staff_headimgurl,b.title as staff_title,b.lat as staff_lat,b.lng staff_lng,b.address as staff_address,b.address1 as staff_address1,b.address2 as staff_address2,b.region as staff_region" +
            " from my_circle a " +
            " left join my_staff b on a.staff_id = b.id \n" +
            "<where>" +
            " and a.status = 1 \n" +
            " and a.utype != 1 \n" +
            " <if test='districtid != null and districtid != \"\" and districtid &gt; 100000'> " +
            "  and COALESCE(b.districtid, a.districtid) = #{districtid} \n" +
            " </if> " +
            " <if test='districtid != null and districtid != \"\" and districtid &lt; 100000 and districtid &gt; 1000'> " +
            "  and COALESCE(b.districtid1, a.districtid1)= #{districtid} \n" +
            " </if> " +
            " <if test='districtid != null and districtid != \"\" and districtid &lt; 1000 and districtid &gt; 0'> " +
            "  and COALESCE(b.districtid2, a.districtid2) = #{districtid} \n" +
            " </if> " +
            " <if test='orConditions != null and orConditions != \"\"'> " +
            " and( ${orConditions} )\n" +
            " </if> " +
            "</where>"+
            " group by a.staff_id order by min_priority asc,id desc \n" +
            " LIMIT #{page},#{pageSize} " +
            " </script>"})
    List<MySearchDto> selectMyCirclePage(@Param("page") Long page, @Param("pageSize") Long pageSize, @Param("districtid") Integer districtid, @Param("orConditions") String orConditions, @Param("orderStr") String orderStr);

    @Select({" <script> select count(*) from ( select staff_id" +
            " from my_circle a " +
            " left join my_staff b on a.staff_id = b.id \n" +
            "<where>" +
            " and a.status =1 \n" +
            " and a.utype != 1 \n" +
            " <if test='districtid != null and districtid != \"\" and districtid &gt; 100000'> " +
            "  and COALESCE(b.districtid, a.districtid) = #{districtid} \n" +
            " </if> " +
            " <if test='districtid != null and districtid != \"\" and districtid &lt; 100000 and districtid &gt; 1000'> " +
            "  and COALESCE(b.districtid1, a.districtid1)= #{districtid} \n" +
            " </if> " +
            " <if test='districtid != null and districtid != \"\" and districtid &lt; 1000 and districtid &gt; 0'> " +
            "  and COALESCE(b.districtid2, a.districtid2) = #{districtid} \n" +
            " </if> " +
            " <if test='orConditions != null and orConditions != \"\"'> " +
            " and( ${orConditions} )\n" +
            " </if> " +
            "</where>" +
            " group by a.staff_id " +
            " ) as total  </script>"})
    Integer selectMyCircleTotal(@Param("districtid") Integer districtid, @Param("orConditions") String orConditions);


    @Select({" <script> select a.*,b.headimgurl as staff_logo,b.logo as staff_headimgurl,b.title as staff_title,b.lat as staff_lat,b.lng staff_lng,b.address as staff_address,b.address1 as staff_address1,b.address2 as staff_address2,b.region as staff_region" +
            " from my_circle a " +
            " left join my_staff b on a.staff_id = b.id \n" +
            " <where> " +
            " and a.status = 1 \n" +
            " <if test= 'utype!=null and utype!=0 and utype !=1'>" +
            " and COALESCE(b.category_utype,a.utype) = ${utype} and a.utype != 1" +
            " </if> " +
            " <if test= 'utype!=null and utype==1'>" +
            " and  b.utype = ${utype} " +
            " </if> " +
            " <if test= 'utype==null or utype==0 '> " +
            " and a.utype != 1  " +
            "</if>  " +
            " <when test= 'staffId!=null and staffId!=0 '>" +
            " and a.staff_id = #{staffId} " +
            " </when> " +
            " <if test='districtid != null and districtid != \"\" and districtid &gt; 100000'> " +
            "  and COALESCE(b.districtid,a.districtid) = #{districtid} \n" +
            " </if> " +
            " <if test='districtid != null and districtid != \"\" and districtid &lt; 100000 and districtid &gt; 1000'> " +
            "  and COALESCE(b.districtid1,a.districtid1) = #{districtid} \n" +
            " </if> " +
            " <if test='districtid != null and districtid != \"\" and districtid &lt; 1000 and districtid &gt; 0'> " +
            "  and COALESCE(b.districtid2,a.districtid2) = #{districtid} \n" +
            " </if> " +
            "</where>" +
            " <when test=' rand != null and rand > 0 '> " +
            " order by rand(#{rand}) \n" +
            " </when> " +
            " <when test=' rand == null or rand == 0 '> " +
            " order by a.create_time desc \n" +
            " </when> " +
            " LIMIT #{page},#{pageSize} " +
            " </script>"})
    List<MyCircleDto> selectListPageToStaff(@Param("page") Long page, @Param("pageSize") Long pageSize, @Param("utype") Integer utype, @Param("staffId") Integer staffId,@Param("districtid") String districtid,@Param("rand") Integer rand);

    @Select({" <script> select count(b.id) cnt " +
            " from my_circle a " +
            " left join my_staff b on a.staff_id = b.id \n" +
            " <where> " +
            " and a.status = 1 \n" +
            " <if test= 'utype!=null and utype!=0 and utype !=1'>" +
            " and COALESCE(b.category_utype,a.utype) = ${utype} and a.utype != 1" +
            " </if> " +
            " <if test= 'utype!=null and utype==1'>" +
            " and  b.utype = ${utype} " +
            " </if> " +
            " <if test= 'utype==null or utype==0 '> " +
            " and a.utype != 1  " +
            "</if>  " +
            " <when test= 'staffId!=null and staffId!=0 '>" +
            " and a.staff_id = #{staffId} " +
            " </when> " +
            " <if test='districtid != null and districtid != \"\" and districtid &gt; 100000'> " +
            "  and COALESCE(b.districtid,a.districtid) = #{districtid} \n" +
            " </if> " +
            " <if test='districtid != null and districtid != \"\" and districtid &lt; 100000 and districtid &gt; 1000'> " +
            "  and COALESCE(b.districtid1,a.districtid1) = #{districtid} \n" +
            " </if> " +
            " <if test='districtid != null and districtid != \"\" and districtid &lt; 1000 and districtid &gt; 0'> " +
            "  and COALESCE(b.districtid2,a.districtid2) = #{districtid} \n" +
            " </if> " +
            "</where>" +
            " </script>"})
    Long selectListCountToStaff(@Param("utype") Integer utype, @Param("staffId") Integer staffId,@Param("districtid") String districtid);
}
